/*
 * noVNC UTF-8 修复补丁
 * 
 * 此文件包含对 clipboardPasteFrom 函数的修复，强制支持UTF-8编码
 * 使用方法：在主HTML文件中替换原始的rfb.js导入
 */

// 导入原始模块
import RFB from './rfb.js';
import { encodeUTF8 } from './util/strings.js';

// 保存原始方法
const originalClipboardPasteFrom = RFB.prototype.clipboardPasteFrom;

// 重写 clipboardPasteFrom 方法以支持UTF-8
RFB.prototype.clipboardPasteFrom = function(text) {
    if (this._rfbConnectionState !== 'connected' || this._viewOnly) { return; }

    console.log('[UTF-8 Fix] clipboardPasteFrom called with text:', text);
    console.log('[UTF-8 Fix] Extended clipboard supported:', 
        this._clipboardServerCapabilitiesFormats[extendedClipboardFormatText] &&
        this._clipboardServerCapabilitiesActions[extendedClipboardActionNotify]);

    if (this._clipboardServerCapabilitiesFormats[extendedClipboardFormatText] &&
        this._clipboardServerCapabilitiesActions[extendedClipboardActionNotify]) {

        // 扩展剪贴板模式 - 原始逻辑保持不变
        console.log('[UTF-8 Fix] Using extended clipboard protocol');
        this._clipboardText = text;
        RFB.messages.extendedClipboardNotify(this._sock, [extendedClipboardFormatText]);
    } else {
        // 修改：强制使用UTF-8编码而不是ISO 8859-1
        console.log('[UTF-8 Fix] Using UTF-8 encoding for standard clipboard');
        
        try {
            // 使用noVNC内置的UTF-8编码函数
            let utf8Data = encodeUTF8(text);
            let data = new Uint8Array(utf8Data.length);
            
            for (let i = 0; i < utf8Data.length; i++) {
                data[i] = utf8Data.charCodeAt(i);
            }
            
            console.log('[UTF-8 Fix] Original text:', text);
            console.log('[UTF-8 Fix] UTF-8 bytes:', Array.from(data));
            console.log('[UTF-8 Fix] Sending', data.length, 'bytes');
            
            RFB.messages.clientCutText(this._sock, data);
            console.log('[UTF-8 Fix] UTF-8 data sent successfully');
            
        } catch (error) {
            console.error('[UTF-8 Fix] Error encoding UTF-8:', error);
            // 如果UTF-8编码失败，回退到原始方法
            console.log('[UTF-8 Fix] Falling back to ISO 8859-1 encoding');
            originalClipboardPasteFrom.call(this, text);
        }
    }
};

// 添加调试方法来检查剪贴板状态
RFB.prototype.getClipboardCapabilities = function() {
    return {
        extendedFormats: this._clipboardServerCapabilitiesFormats,
        extendedActions: this._clipboardServerCapabilitiesActions,
        hasExtendedClipboard: !!(this._clipboardServerCapabilitiesFormats[extendedClipboardFormatText] &&
                                this._clipboardServerCapabilitiesActions[extendedClipboardActionNotify])
    };
};

// 添加强制使用扩展剪贴板的方法
RFB.prototype.forceExtendedClipboard = function() {
    console.log('[UTF-8 Fix] Attempting to force extended clipboard support');
    
    // 模拟扩展剪贴板能力
    this._clipboardServerCapabilitiesFormats = this._clipboardServerCapabilitiesFormats || {};
    this._clipboardServerCapabilitiesActions = this._clipboardServerCapabilitiesActions || {};
    
    this._clipboardServerCapabilitiesFormats[extendedClipboardFormatText] = true;
    this._clipboardServerCapabilitiesActions[extendedClipboardActionNotify] = true;
    this._clipboardServerCapabilitiesActions[extendedClipboardActionRequest] = true;
    this._clipboardServerCapabilitiesActions[extendedClipboardActionProvide] = true;
    
    console.log('[UTF-8 Fix] Extended clipboard capabilities forced');
    console.log('[UTF-8 Fix] Current capabilities:', this.getClipboardCapabilities());
};

export default RFB;
